*------------------------------------------------------------------------------
* ANES 2006 : combine ANES 2004 + ANES 2006 to identify some individual characteristics 
*==============================================================================

* ---------------- data ANES 2004 : to link to ANES 2006
use "${dir_raw}/anes2004TS.dta", clear

gen mid = V040001
*gen r_female_04 = V041109a == 2 if ~missing(V041109a)

gen r_ideo0 = V043086 
gen r_ideo1 = V043085 if V043085 < 10
gen r_ideo2 = V043085a if V043085a < 8

gen r_ideo = r_ideo1 
	replace r_ideo = r_ideo2 if missing(r_ideo)

gen r_wrkstat = V043260b if V043260b < 9
	recode r_wrkstat (1=1) (else=0), gen(r_working)
	recode r_wrkstat (2 4=1) (else=0), gen(r_unemployed)
	recode r_wrkstat (5=1) (else=0), gen(r_retired)
	recode r_wrkstat (6 7 8=1) (else=0), gen(r_otherworks)

recode V043299 (10=2) (50=1) (else=3), gen(r_race)
	replace r_race = . if V043299 == 88 
	replace r_race = . if V043299 == 89

	gen r_white = r_race == 1 if ~missing(r_race)
	gen r_black = r_race == 2 if ~missing(r_race)
	gen r_others = r_race == 3 if ~missing(r_race)
tab V043251

gen r_marstat = V043251 if V043251 < 8
	gen r_married = r_marstat == 1 if ~missing(r_marstat)
	gen r_single  = r_marstat == 5 if ~missing(r_marstat)

gen r_degree = V043254
	replace r_degree = . if r_degree == 9
	recode r_degree (1=8) (2=11) (3=12) (4=13) (5=14) (6=16) (7=18) (9=.), gen(r_educ)

gen r_talk_politics = V045153 == 1 if V045153  < 9

keep mid r_* 

save "${dir_processed}/anes2004_merge.dta", replace 

* ---------------- data ANES 2006 
use "${dir_raw}/anes2006pilot.dta",clear
gen id= V06P001
gen mid = id 

merge 1:1 mid using "${dir_processed}/anes2004_merge.dta", gen(m_2004)

keep if m_2004 == 3 
drop mid 

* ------- respondent characteristics 
gen r_sex = V06P005
	gen r_female = V06P005 == 2 if ~missing(V06P005)

gen r_age = V06P006


gen wtall = V06P002 
gen sampcode = V06P007b 
gen vpsu = V06P007a 
gen intid = V06P301 

gen year = 2006
gen str_dateintv = V06P201c 
replace str_dateintv = V06P202c  if V06P202c != ""

gen month = "" 
replace month = substr(str_dateintv,1,2)

gen day = ""
replace day = substr(str_dateintv, 3,2)

gen yy = year 
replace yy = 2007 if year == 2006 & month == "01"

gen svydate = day+"/"+month+"/"+string(yy)
gen svydate2 = date(svydate, "DMY")

gen module = V06P406 // things that were important, government and election
gen i_imp = module == 1 
gen n_size = V06P588 // wording order democract first or republican first 
	replace n_size = . if n_size == 99 
	*replace n_size = 0 if V06P587 == 5 
	*replace n_size = . if V06P587 == 8 
gen r_no_religion = V06P558 == 7 if V06P558 < 8
gen r_attendance = V06P554x  if V06P554x  < 8 
*0. More often than once a week |         74       10.96       10.96
*                 1. Every week |         93       13.78       24.74
*          2. Almost every week |         94       13.93       38.67
*      3. Once or twice a month |        101       14.96       53.63
*         4. A few times a year |         87       12.89       66.52
*          5. R does not attend |        223       33.04       99.56
*                 8. Don't know |          1        0.15       99.70
*                    9. Refused |          2        0.30      100.00

gen r_partyid = V06P680  if V06P680 < 9
	gen r_partyid7 = r_partyid + 1 if r_partyid < 7

des V06P587-V06P629

* ideo 
gen a1_close = V06P590
gen a1_talkpol_day = V06P591x

gen a2_close = V06P592
gen a2_talkpol_day = V06P593x

recode a2_close 8=. 
recode a2_talkpol 8888 = .

gen a3_close = V06P594
gen a3_talkpol_day = V06P595x

sum a1_* a2_* a3_*

gen a1_partyid = V06P603x 
gen a2_partyid = V06P608x 
gen a3_partyid = V06P613x 

forvalues i = 1/3{
	gen a`i'_partyid7 = a`i'_partyid + 1 if  a`i'_partyid  < 7
}


gen dataset = "ANES"

lookfor state 
gen fips_state = V06P102 
gen fips_county = V06P107

*gen state= V06P102a 
gen ni = V06P587 == 5 if V06P587 < 8 

keep id sampcode vpsu wtall module intid svydate svydate2 year month day dataset /*
*/ n_size ni r_* i_* a* fips_state fips_county  

saveold "${dir_processed}/anes_2006.dta", replace version(12)

